//
//  HuisuoJidiVc.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/3/15.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "HuisuoJidiVc.h"
#import <MJRefresh.h>
#import "HuisuoJidiCell.h"
#import "NewHomeNetwork.h"
#import "MBProgressHUD+MJ.h"
#import "ConNewMapVc.h"
#import "ConeBaseViewController.h"

@interface HuisuoJidiVc ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,assign) NSInteger page;
@property (nonatomic,strong) NSMutableArray *sourceLists;

@end

@implementation HuisuoJidiVc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    if (self.type == 3) {
        self.title = @"会所";
    }else if (self.type == 2) {
        self.title = @"基地";
    }
    [self setUpNavi];
    [self.view addSubview:self.tableView];
    self.tableView.mj_footer.hidden = YES;
    [self.tableView.mj_header beginRefreshing];
}
- (void)setUpNavi
{
    UIButton *mapBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    [mapBtn setImage:[UIImage imageNamed:@"地图"] forState:UIControlStateNormal];
    [mapBtn addTarget:self action:@selector(mapClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:mapBtn];
}
- (void)mapClick
{
    ConNewMapVc *vc = [[ConNewMapVc alloc]init];
    vc.type = self.type;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma ----------------tableView-----------------------------------
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc]init];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sourceLists.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 25+160*ScreenW/750;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HuisuoJidiCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HuisuoJidiCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.sourceLists[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ConeBaseViewController *vc = [[ConeBaseViewController alloc]init];
    vc.mapModel = self.sourceLists[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
//基地会所列表type 2基地 3会所
- (void)getBaseList
{
    [self showWaiter];
    NSMutableDictionary *parames = [NSMutableDictionary dictionary];
    parames[@"type"] = @(self.type);
    [NewHomeNetwork getBaseListWithParame:parames response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWaiter];
        [self.tableView.mj_header endRefreshing];
        if (success) {
            [self.sourceLists addObjectsFromArray:lists];
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-SafeAreaTopHeight-SafeAreaBottomHeight)];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HuisuoJidiCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([HuisuoJidiCell class])];
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#EFEFF4"];
        _tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 0);
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = 1;
            [self.sourceLists removeAllObjects];
            [self getBaseList];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.page++;
            [self getBaseList];
        }];
    }
    return _tableView;
}
- (NSMutableArray *)sourceLists
{
    if (!_sourceLists) {
        _sourceLists = [NSMutableArray array];
    }
    return _sourceLists;
}

@end
